package com.mapler.neteller.ui;

import com.mapler.ui.*;
import com.mapler.model.AdModel;
import com.mapler.model.SModel;
import com.mapler.model.UAModel;
import com.mapler.neteller.service.NetellerSignupWorker;
import com.mapler.service.INotifier;
import com.mapler.utility.HttpHelper;
import com.mapler.utility.IConstant;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import javax.swing.SwingUtilities;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;

/**
 *
 * @author none
 */
public class EngineNetellerSignupUI extends javax.swing.JPanel implements INotifier {

    public ScheduledExecutorService emailExecutor = Executors.newScheduledThreadPool(10);
    protected ScheduledFuture<?> emailFuture = null;
    private String username;
    private String password;
    private String country;
    private SModel sModel;
    private ArrayList<UAModel> uaModels;
    private ArrayList<AdModel> addModels;
    private HashMap<String, UAModel> usHashMap;
    private static Logger log = Logger.getLogger(EngineUI.class);
    private INotifier iNotifier;
    private NetellerSignupWorker netellerSignupWorker;

    public EngineNetellerSignupUI(String username, String password, String country) {
        this.username = username;
        this.password = password;
        this.country = country;
        initComponents();
    }

    public SModel getsModel() {
        if (sModel == null) {
            sModel = new SModel();
        }
        return sModel;
    }

    public void setsModel(SModel sModel) {
        this.sModel = sModel;
    }

    public ArrayList<UAModel> getUaModels() {
        if (uaModels == null) {
            uaModels = new ArrayList<UAModel>();
        }
        return uaModels;
    }

    public void setUaModels(ArrayList<UAModel> uaModels) {
        this.uaModels = uaModels;
    }

    public ArrayList<AdModel> getAddModels() {
        if (addModels == null) {
            addModels = new ArrayList<AdModel>();
        }
        return addModels;
    }

    public void setAddModels(ArrayList<AdModel> addModels) {
        this.addModels = addModels;
    }

    public HashMap<String, UAModel> getUsHashMap() {
        if (usHashMap == null) {
            usHashMap = new HashMap<String, UAModel>();
        }
        return usHashMap;
    }

    public void setUsHashMap(HashMap<String, UAModel> usHashMap) {
        this.usHashMap = usHashMap;
    }

    /**
     * Creates new form EngineUI
     */
    public EngineNetellerSignupUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        start = new javax.swing.JButton();
        end = new javax.swing.JButton();
        driver = new javax.swing.JComboBox();
        message = new javax.swing.JLabel();
        jBStop = new javax.swing.JButton();
        machine = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        isLogout = new javax.swing.JCheckBox();
        jTFProxyServer = new javax.swing.JTextField();
        jTFProxyPort = new javax.swing.JTextField();
        jCBProxyType = new javax.swing.JCheckBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jCBUseProxy = new javax.swing.JCheckBox();

        jLabel4.setText("jLabel4");

        start.setText("Start");
        start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startActionPerformed(evt);
            }
        });

        end.setText("Stop & Exit");
        end.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                endActionPerformed(evt);
            }
        });

        driver.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "FF", "IE", "GC" }));

        jBStop.setText("Stop");
        jBStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBStopActionPerformed(evt);
            }
        });

        jLabel1.setText("Machine");

        isLogout.setText("Do Logout");

        jCBProxyType.setSelected(true);
        jCBProxyType.setText("Proxy Type (Default Manual)");

        jLabel2.setText("Proxy Server");

        jLabel3.setText("Proxy Port");

        jCBUseProxy.setSelected(true);
        jCBUseProxy.setText("Use Proxy");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(message, javax.swing.GroupLayout.DEFAULT_SIZE, 587, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(start)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBStop)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(end)
                        .addGap(175, 175, 175))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(195, 195, 195)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCBProxyType)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jTFProxyPort)
                                .addComponent(driver, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(machine, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE)
                                .addComponent(isLogout)
                                .addComponent(jTFProxyServer))
                            .addComponent(jCBUseProxy))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39)
                .addComponent(jCBUseProxy)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(isLogout)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(machine, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(driver, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFProxyServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFProxyPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCBProxyType)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(start)
                    .addComponent(end)
                    .addComponent(jBStop))
                .addGap(70, 70, 70))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startActionPerformed
        this.getsModel().setUsername(username);
        sModel.setPassword(password);
        sModel.setCountry(country);
        sModel.setDriver("FF");
        sModel.setProxy(machine.getText());
        sModel.setLogout(isLogout.isSelected());
        sModel.setProxyServer(jTFProxyServer.getText());
        if(jTFProxyPort.getText()!=null && !jTFProxyPort.getText().isEmpty()) {
            sModel.setProxyPort(Integer.parseInt(jTFProxyPort.getText()));
        }
        sModel.setUseSystem(true);
        sModel.setProxyType(jCBProxyType.isSelected());
        sModel.setBrowserProxy(jCBUseProxy.isSelected());
        netellerSignupWorker = new NetellerSignupWorker(this.getiNotifier(), this.getsModel());
        netellerSignupWorker.update();
    }//GEN-LAST:event_startActionPerformed

    private void endActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_endActionPerformed
        try {
            String uri = "http://" + IConstant.HOSTNAME + "/index.php?r=netellerUser/logout&username=" + this.username + "&password=" + this.password;
            String response = HttpHelper.post(new URL(uri), "");

            Document document = DocumentHelper.parseText(response);
            String errorCode = document.valueOf("Response/errorcode");
            if (errorCode.equalsIgnoreCase("000")) {
                String msg = document.valueOf("Response/message");
                log.debug("Problem in logout. causes " + msg);
            } else {
                log.debug("Signout perfectly ");
            }
            Runtime.getRuntime().exec("taskkill /F /IM java.exe");
          
        } catch (Throwable ex) {
            log.debug("Error " + ex);
        }
    }//GEN-LAST:event_endActionPerformed

    private void jBStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBStopActionPerformed
       this.getsModel().setStopEngine(true);
    }//GEN-LAST:event_jBStopActionPerformed

    public void initSettings() {
        this.setsModel(null);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox driver;
    private javax.swing.JButton end;
    private javax.swing.JCheckBox isLogout;
    private javax.swing.JButton jBStop;
    private javax.swing.JCheckBox jCBProxyType;
    private javax.swing.JCheckBox jCBUseProxy;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTFProxyPort;
    private javax.swing.JTextField jTFProxyServer;
    private javax.swing.JTextField machine;
    private javax.swing.JLabel message;
    private javax.swing.JButton start;
    // End of variables declaration//GEN-END:variables

    @Override
    public void notify(final String info) {
        try {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    message.setText(info);
                }
            });
        } catch (Throwable ex) {
            log.debug("notify:: Error causes " + ex.getMessage());
        }
    }

    public INotifier getiNotifier() {
        if (iNotifier == null) {
            iNotifier = this;
        }
        return iNotifier;
    }

    public void setiNotifier(INotifier iNotifier) {
        this.iNotifier = iNotifier;
    }

    @Override
    public void write(String info) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
